//
//  SN_Question_Detail_BottomView.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/22.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_Question_Detail_BottomView.h"

@interface SN_Question_Detail_BottomView()


@property (nonatomic, strong) UIButton *leftArrowButton; //左箭头
@property (nonatomic, strong) UIButton *rightArrowButton; //右箭头



@end

@implementation SN_Question_Detail_BottomView {
    
    CGFloat _btnW;
    
    UIButton *_lastSelectBtn;
    
    NSInteger _allCount;
}


- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        
        _catalogButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_catalogButton setTitle:@"目录" forState:UIControlStateNormal];
        _catalogButton.titleLabel.font = [UIFont systemFontOfSize:17];
        [_catalogButton setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateNormal];
        [_catalogButton setFrame:CGRectMake(10, 17, 40, 20)];
        
        [self addSubview:_catalogButton];
        
        _leftArrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftArrowButton setImage:[UIImage imageNamed:@"listmore_arrowleft"] forState:UIControlStateNormal];
        _leftArrowButton.frame = CGRectMake(73, 15, 40, 20);
        [_leftArrowButton addTarget:self action:@selector(leftArrowClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftArrowButton];
        
        _rightArrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightArrowButton setImage:[UIImage imageNamed:@"listmore_arrowleft"] forState:UIControlStateNormal];
        _rightArrowButton.imageView.transform = CGAffineTransformMakeRotation(M_PI);
        _rightArrowButton.frame = CGRectMake(SCREENWIDTH - 18 - 40, 15, 40, 20);
        [_rightArrowButton addTarget:self action:@selector(rightArrowClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightArrowButton];
        
        CGFloat scrollX = CGRectGetMaxX(_leftArrowButton.frame);
        
        
        CGFloat scrollW = SCREENWIDTH - scrollX - 18 - 40;
        
        CGFloat btnW = scrollW / 4;
        _btnW = btnW;

        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(scrollX, 15, scrollW, 20)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scrollView];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 0.5)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#dedede"];
        [self addSubview:lineView];
    }
    
    return self;
}

- (void)setDataArr:(NSMutableArray *)dataArr {
    
    _dataArr = dataArr;
    NSInteger allCount = 0; //题目总数量
    
    /* _selectIndex 是大题下选择的小题序号,所以总序号需要加上前面大题的数量 */
    NSInteger allSelectedIndex = 0;
    for (SN_Question_SolveList_Model *listModel in dataArr) {
        
        if (_listModel == listModel) { //当前的选择的大题
         
            allSelectedIndex = allCount + _selectIndex;
        }
        
        allCount += listModel.questionlist.count;
        _allCount = allCount;
    }
    
    _scrollView.contentSize = CGSizeMake(_btnW * allCount, 20);
    
    if (allSelectedIndex + 1 > 4) {
        
        NSInteger mom = (allSelectedIndex + 1) % 4;
        NSInteger rol = (allSelectedIndex + 1) / 4;
        
        _scrollView.contentOffset = CGPointMake((CGFloat) (rol - 1) * _scrollView.width+ mom * _btnW, 0);
    }
    
    int count = 0;
    
    for (int i = 0; i < self.dataArr.count; i++) {
        
        SN_Question_SolveList_Model *listModel = [self.dataArr objectAtIndex:i];
     
        
        for (int j = 0; j < listModel.questionlist.count; j++) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            
            btn.frame = CGRectMake(_btnW * count, 0.5, _btnW, 20);
            btn.tag = i * 1000 + j; //1001 1002 1003  2001 2002  千位代表大题题号
            btn.titleLabel.font = [UIFont systemFontOfSize:17];
            [btn setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor colorWithHexString:@"#fb624a"] forState:UIControlStateSelected];
            
            NSString *titleStr = [NSString stringWithFormat:@"%d.%d",i+1,j+1];
            [btn setTitle:titleStr forState:UIControlStateNormal];
            
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [_scrollView addSubview:btn];
            
            
            if (_listModel == listModel) {
                
                if (j == _selectIndex) {
                    
                    btn.selected = YES;
                    _lastSelectBtn = btn;
                }
            }
            
            count++;
            
        }
        
    }
    
}


- (void)setListModel:(SN_Question_SolveList_Model *)listModel {
    
    _listModel = listModel;
    
    
}

- (void)btnClick:(UIButton *)sender {
    
    _lastSelectBtn.selected = NO;
    
    sender.selected = YES;
    _lastSelectBtn = sender;
    
    NSInteger tag = sender.tag;
    
    NSInteger bigIndex = tag / 1000; //大题序号
    NSInteger littleIndex = tag % 1000; //小题序号
    SN_Question_SolveList_Model *listModel = [self.dataArr objectAtIndex:bigIndex];
    SN_Question_Solve_Model *model = [listModel.questionlist objectAtIndex:littleIndex];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(questionBtnClick:)]) {
        
        [self.delegate questionBtnClick:model];
    }
}

- (void)leftArrowClick {
    
    CGFloat x = _scrollView.contentOffset.x;
    
    if (x >= _btnW) {
        
        x -= _btnW;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            _scrollView.contentOffset = CGPointMake(x, 0);
        }];
        
    }
}

- (void)rightArrowClick {
    
    _scrollView.contentSize = CGSizeMake(_btnW * _allCount, 20);
    
    CGFloat x = _scrollView.contentOffset.x;
    
    CGFloat maxX = _scrollView.contentSize.width - _scrollView.width;
    
    if (x < maxX) {
        
        x += _btnW;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            _scrollView.contentOffset = CGPointMake(x, 0);
        }];
        
    }
    
}

@end
